package com.zy.fee.roadpark.activity.nb;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.zy.fee.roadpark.R;
import com.zy.fee.roadpark.adapter.MessageAdapter;
import com.zy.fee.roadpark.config.Constants;
import com.zy.fee.roadpark.dao.MessageLogic;
import com.zy.fee.roadpark.model.M_Message;
import com.zy.fee.roadpark.model.apimodel.APIM_MessageList;
import com.zygk.library.base.BaseActivity;
import com.zygk.library.util.HttpRequest;
import com.zygk.library.util.LibraryHelper;
import com.zygk.library.util.ToastUtil;
import com.zygk.library.view.SmoothListView.SmoothListView;
import java.util.ArrayList;
import java.util.List;

/**
 * 消息列表
 * Created by qhj on 2018/9/13 0013.
 */

public class MessageListActivity extends BaseActivity
    implements SmoothListView.ISmoothListViewListener {
  @BindView(R.id.lh_tv_title) TextView lhTvTitle;
  @BindView(R.id.smoothListView) SmoothListView mSmoothListView;
  @BindView(R.id.rl_no_data) RelativeLayout rlNoData;

  private MessageAdapter adapter;
  private int page = 0;

  @Override public void setContentView() {
    setContentView(R.layout.activity_message);
  }

  @Override public void init() {
    initData();
    initView();
    initListener();
  }

  private void initData() {
    adapter = new MessageAdapter(mContext, new ArrayList<>());
    mSmoothListView.setAdapter(adapter);
    registerReceiver(new String[] { Constants.BROADCAST_REFRESH_MESSAGE_LIST });
  }

  private void initView() {
    lhTvTitle.setText("消息列表");

    mSmoothListView.setSmoothListViewListener(this);
    mSmoothListView.setRefreshEnable(true);
    mSmoothListView.setLoadMoreEnable(false);

    nbnode_message_list(false);
  }

  private void initListener() {
    mSmoothListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        M_Message message = adapter.getItem(i - 1);
        MessageLogic.nbnode_message_doRead(mContext, message.getMessageID(),
            new HttpRequest.HttpCallback() {
              @Override public void onStart() {
                showPd();
              }

              @Override public void onSucceed(Object obj) {
                message.setState(1);
                adapter.notifyDataSetChanged();
                if (message.getFatherType() == 1) {
                  if (message.getChildType() == 1) {
                    startActivity(new Intent(mContext, NbRegistrationPlateActivity.class));
                  } else if (message.getChildType() == 2) {
                    startActivity(new Intent(mContext, NbReceivablesActivity.class));
                  }
                }
              }

              @Override public void onFailed() {
                ToastUtil.showNetErrorMessage(mContext);
              }

              @Override public void onFinish() {
                dismissPd();
              }
            });
      }
    });
  }

  private void nbnode_message_list(final boolean isLoadMore) {
    MessageLogic.nbnode_message_list(mContext, LibraryHelper.userManager().getUserID(),
        isLoadMore ? ++page : (page = 1), Constants.ROWS, new HttpRequest.HttpCallback() {
          @Override public void onStart() {
            showPd();
          }

          @Override public void onSucceed(Object obj) {
            APIM_MessageList result = (APIM_MessageList) obj;
            fillAdapter(result.getMessageList(), result.getMaxpage(), isLoadMore);
          }

          @Override public void onFailed() {
            ToastUtil.showNetErrorMessage(mContext);
          }

          @Override public void onFinish() {
            dismissPd();
            mSmoothListView.stopRefresh();
            mSmoothListView.stopLoadMore();
          }
        });
  }

  // 填充数据
  private void fillAdapter(List<M_Message> list, int maxPage, boolean isLoadMore) {
    if (list == null || list.size() == 0) {
      rlNoData.setVisibility(View.VISIBLE);
      mSmoothListView.setVisibility(View.GONE);
      mSmoothListView.setLoadMoreEnable(false);
    } else {
      rlNoData.setVisibility(View.GONE);
      mSmoothListView.setVisibility(View.VISIBLE);
      mSmoothListView.setLoadMoreEnable(page < maxPage);
      adapter.setData(list, isLoadMore);
    }
  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);
  }

  @Override protected void handleReceiver(Context context, Intent intent) {
    super.handleReceiver(context, intent);
    if (intent == null || TextUtils.isEmpty(intent.getAction())) {
      return;
    }
    if (Constants.BROADCAST_REFRESH_MESSAGE_LIST.equals(intent.getAction())) {
      nbnode_message_list(false);
    }
  }

  @OnClick(R.id.ll_back) public void onViewClicked() {
    finish();
  }

  @Override public void onRefresh() {
    nbnode_message_list(false);
  }

  @Override public void onLoadMore() {
    nbnode_message_list(true);
  }
}
